{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3cceaab5-78fa-4411-9996-4f6ff26385cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9923898-7936-4cc6-9044-aee87238ed6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series([1,3,5,np.nan,44,1])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebe40bc3-59c7-4cb0-aa7a-78feca6439d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20250414',periods=6)\n",
    "print(dates)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cb3f310-d6c1-482b-8d2b-e2f9183ee8b7",
   "metadata": {},
   "source": [
    "**DataFrame**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea14e35b-acb7-481a-a500-aaebb2e7067d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## DataFrame 是二维的数组\n",
    "## 初始化DataFrame\n",
    "#方法1\n",
    "df0 = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d']) # index 和 columns 分别是行和列的索引\n",
    "print(df0)\n",
    "df1 = pd.DataFrame(np.random.randn(6,4)) # 行和列的默认索引是 0 1 2 ...\n",
    "print(df1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b587e1a-a0fa-4f6d-ae7b-5569850e4934",
   "metadata": {},
   "outputs": [],
   "source": [
    "#方法2,手动输入每一列数据\n",
    "df2 = pd.DataFrame({\n",
    "    'A': [1.,5.,3.,4.],  # 浮点数\n",
    "    'B': pd.Timestamp('20250414'),  # 时间戳\n",
    "    'C': pd.Series(1, index=list(range(4)), dtype='float32'),  # Series对象\n",
    "    'D': np.array([3] * 4, dtype='int32'),  # NumPy数组\n",
    "    'E': pd.Categorical(['test', 'train', 'test', 'train']),  # 分类数据\n",
    "    'F': \"foo\"  # 字符串\n",
    "})\n",
    "\n",
    "# 输出DataFrame\n",
    "print(\"DataFrame内容：\")\n",
    "print(df2)\n",
    "\n",
    "# 输出每一列的数据类型\n",
    "print(\"\\n每一列的数据类型：\")\n",
    "print(df2.dtypes)\n",
    "\n",
    "# 输出索引\n",
    "print(\"\\n索引：\")\n",
    "print(df2.index)\n",
    "\n",
    "# 输出列名\n",
    "print(\"\\n列名：\")\n",
    "print(df2.columns)\n",
    "\n",
    "# 输出描述(只能描述数字类型，其他类型会被忽略)\n",
    "print(\"\\n描述：\")\n",
    "print(df2.describe())\n",
    "\n",
    "# 输出转置\n",
    "print(\"\\n转置：\")\n",
    "print(df2.T)\n",
    "\n",
    "# 排序\n",
    "print(\"\\n索引排序：\")\n",
    "print(df2.sort_index(ascending=False,axis=1)) # ascending 指定顺序或者倒序\n",
    "\n",
    "print(\"\\n数值排序：\")\n",
    "print(df2.sort_values(by='A')) # ascending 指定顺序或者倒序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ec6a78d-099c-4c86-84d5-9d20d0a0912a",
   "metadata": {},
   "source": [
    "**选择数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f7f6a348-7275-4eff-aaa8-10a36e9afd41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             A   B   C   D\n",
      "2025-04-15   0   1   2   3\n",
      "2025-04-16   4   5   6   7\n",
      "2025-04-17   8   9  10  11\n",
      "2025-04-18  12  13  14  15\n",
      "2025-04-19  16  17  18  19\n",
      "2025-04-20  20  21  22  23\n",
      "2025-04-15     0\n",
      "2025-04-16     4\n",
      "2025-04-17     8\n",
      "2025-04-18    12\n",
      "2025-04-19    16\n",
      "2025-04-20    20\n",
      "Freq: D, Name: A, dtype: int64 \n",
      " 2025-04-15     0\n",
      "2025-04-16     4\n",
      "2025-04-17     8\n",
      "2025-04-18    12\n",
      "2025-04-19    16\n",
      "2025-04-20    20\n",
      "Freq: D, Name: A, dtype: int64\n",
      "\n",
      "\n",
      "            A  B   C   D\n",
      "2025-04-15  0  1   2   3\n",
      "2025-04-16  4  5   6   7\n",
      "2025-04-17  8  9  10  11 \n",
      "              A   B   C   D\n",
      "2025-04-16   4   5   6   7\n",
      "2025-04-17   8   9  10  11\n",
      "2025-04-18  12  13  14  15\n"
     ]
    }
   ],
   "source": [
    "dates = pd.date_range('20250415',periods=6)\n",
    "df = pd.DataFrame(np.arange(24).reshape((6,4)),index = dates,columns=['A','B','C','D'])\n",
    "print(df)\n",
    "\n",
    "## 输出某一列\n",
    "print(df['A'],'\\n',df.A) # 两种输出基本一致\n",
    "\n",
    "## 选择某些行\n",
    "print('\\n')\n",
    "print(df[0:3],'\\n',df['20250416':'20250418'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6a0970be-162d-4bcc-984d-5a909b08766e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    16\n",
      "B    17\n",
      "C    18\n",
      "D    19\n",
      "Name: 2025-04-19 00:00:00, dtype: int64\n",
      "             A   B\n",
      "2025-04-15   0   1\n",
      "2025-04-16   4   5\n",
      "2025-04-17   8   9\n",
      "2025-04-18  12  13\n",
      "2025-04-19  16  17\n",
      "2025-04-20  20  21\n"
     ]
    }
   ],
   "source": [
    "## 通过label:loc选择数据\n",
    "print(df.loc['20250419'])\n",
    "print(df.loc[:,['A','B']]) # 选择所有行，选择 A B 列\n",
    "print('\\n')\n",
    "print(df.loc(['20']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c603ed0-866f-410c-a61e-a0e67ad23ef6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
